package com.byxy.dao.impl;

import java.io.Serializable;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.SessionFactory;
import org.hibernate.criterion.Projections;
import org.springframework.stereotype.Repository;

import com.byxy.dao.BookDao;
import com.byxy.entity.Book;
@Repository
public class BookDaoImpl implements BookDao {
	@Resource
	private SessionFactory sessionFactory;
	@Override
	public void add(Book book) {
		sessionFactory.getCurrentSession().save(book);
	}

	@Override
	public void delete(Book book) {
		sessionFactory.getCurrentSession().delete(book);
	}

	@Override
	public void update(Book book) {
		sessionFactory.getCurrentSession().update(book);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Book> find() {
		return sessionFactory.getCurrentSession().createCriteria(Book.class).list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Book> find(int pos, int rows) {
		return sessionFactory.getCurrentSession().createCriteria(Book.class)
				.setFirstResult(pos).setMaxResults(rows).list();
	}

	@Override
	public long count() {
		return (long) sessionFactory.getCurrentSession().createCriteria(Book.class).setProjection(Projections.rowCount()).uniqueResult();
	}

	@Override
	public Book getById(Serializable id) {
		return (Book) sessionFactory.getCurrentSession().get(Book.class, id);
	}

}
